package com.holy.sh.couviewholy.pic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.holy.sh.couviewholy.R;

/**
 * Holy Email :victorhhl@163.com
 * Create by Holy on 2019/8/12
 * Description :
 */
public class TelescopeView extends View {
    private Paint mPaint;
    private Bitmap mBitmap;

    public TelescopeView(Context context) {
        super(context);
    }

    public TelescopeView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        initOp();
    }

    public TelescopeView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initOp() {
         mPaint = new Paint();
                mPaint.setColor(Color.BLACK);
                mPaint.setStrokeWidth(2);
                mPaint.setAntiAlias(true);
               // mPaint.setStrokeMiter(90);//设置画笔倾斜度
                //mPaint.setPathEffect();//设置路径样式 CopmosePathEffect-》》合并路径类型用的
                // CornerPathEffect，DashPathEffect，DiscreteOathEffect，pathDashPathEffect,SumpathEffect
                //mPaint.setStrokeCap(); Cap.ROUND`SQUARE`BUTT
                //mPaint.setStrokeJoin(Paint.Join.BEVEL); //设置路径转角样式 MITER·ROUND·BEVEL
                //mPaint.setDither(true);//抗抖动，颜色丰富无法显示，过度显示马赛克相邻像素中间插入过渡值

                //Paint 字体相关
                //mPaint.setTextSize();
                mPaint.setFakeBoldText(false);//加粗
                mPaint.setStrikeThruText(false);//删除线
                mPaint.setUnderlineText(false);//下划线
                //mPaint.setTextAlign(false);//开始绘图点位置？
                //mPaint.setTextScaleX();//设置水平拉伸
                //mPaint.setTextSkewX();//设置水平斜度
                //mPaint.setTypeface();设置字体样式
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
    }

    int mDx,mDy;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDx= (int) event.getX();
                mDy= (int) event.getY();

                postInvalidate();

               return true;
            case MotionEvent.ACTION_MOVE:
                mDx= (int) event.getX();
                mDy= (int) event.getY();
                   break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mDx=-1;
                mDy=-1;
                    break;


        }
       postInvalidate();
        return super.onTouchEvent(event);
    }

    Bitmap bitMapBG;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bitMapBG == null) {
            bitMapBG = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvasbg=new Canvas(bitMapBG);
            canvasbg.drawBitmap(mBitmap,null,new Rect(0,0,getWidth(),getHeight()),mPaint);
        }

        if (mDx != -1 && mDy != -1) {
            mPaint.setShader(new BitmapShader(bitMapBG, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
            canvas.drawCircle(mDx,mDy,150,mPaint);
        }
    }
}
